import Vue from 'vue'
import Vuex from 'vuex'
import actions from './actions'
Vue.use(Vuex)

const state = {
  // 登录状态
  isLogin : window.sessionStorage.getItem('isLogin') !== 'true' ? false :  true ,
  // 账号信息
  account:{},
  // 用户信息
  profile:{},
  // 喜欢音乐列表
  likeIdList:[],
  // 我的歌单，包括创建收藏
  myPlayList:[],
  //播放列表
  musicList:[],
  //当前播放音乐的id
  currenMusicId:0,
  //当前音乐在音乐列表的index
  currenIndex : 0,
  //播放的type
  playType : 'music',
  //播放的状态
  isPlay:false,
  //播放音乐的总时间和当前时间
  currenMusicInfo:{
    totalTime : 0,
    currenTime : 0
  },
  //播放历史列表
  historyList:[],
}
const mutations = {
  setLoginInfo(state , loginInfo){
    state.account = loginInfo.account;
    state.profile = loginInfo.profile;

  },
  setIsLogin(state , isLogin){
    state.isLogin = isLogin;
    if(isLogin){
      window.sessionStorage.setItem('isLogin',true);
    } else {
      window.sessionStorage.removeItem('isLogin');
    }
  },
  setLikeIdList(state,payLoad){
    if(payLoad.type === 'get'){
      state.likeIdList = payLoad.data;
    } else if(payLoad.type === 'unshift'){
      state.likeIdList.unshift(payLoad.data);
    } else if(payLoad.type === 'remove'){
      state.likeIdList.splice(
        state.likeIdList.indexOf(payLoad.data) , 1
      )
    }
  },
  setMyPlayList(state,list){
    state.myPlayList = list
  },
  setMusicList(state,musicList){
    state.musicList = musicList;
  },
  setCurrenMusicId(state , currenMusicId){
    state.currenMusicId = currenMusicId;
  },
  setCurrenIndex(state,currenIndex){
    state.currenIndex = currenIndex
  },
  setPlayType(state,playType){
    if(playType === 'music' || playType === 'personalFm'){
      state.playType = playType;
    }
  },
  setPlayState(state , isPlay){
    state.isPlay = isPlay;
  },
  setTotalTime(state , totalTime){
    state.currenMusicInfo.totalTime = totalTime;
  },
  setCurrenTime(stata,currenTime){
    state.currenMusicInfo.currenTime = currenTime;
  },
  setPersonalFm(state , payload){
    if(payload.type === 'set'){
      state.musicList = payload.data;
      state.currenIndex = 0;
      state.currenMusicId = payload.data[0].id;
    } else if(payload.type === 'next'){
      state.currenIndex++;
      state.currenMusicId = state.musicList[state.currenIndex].id;
    }
  },
  setHistoryList(state , payload){
    if(payload.type === 'unshift'){
      if(state.historyList.findIndex((item) => item.id == payload.data.id) !== -1) return;
      state.historyList.unshift(payload.data);
      if(state.historyList.length > 50){
        state.historyList.splice(-1 , 1);
      }
      window.localStorage.setItem(
        'historylist',JSON.stringify(state.historyList)
      )
      return;
    } else if(payload.type === 'get'){
      state.historyList = payload.data;
    }else if(payload.type === 'clear'){
      
      state.historyList = [];
      window.localStorage.removeItem('historylist');
      return;
    }
  }
}

const getters = {
  // 创建的歌单
  creatPlayList(state){
    return state.isLogin ? state.myPlayList.filter((item) => item.userId === state.profile.userId) : [];
  },
  // 收藏的歌单
  subPlyayList(state){
    return state.isLogin ? state.myPlayList.filter((item) => item.userId !== state.profile.userId) : [];
  },
  userId(state){
    return state.isLogin ? state.profile.userId : 0;
  }
}

const store = new Vuex.Store({
  state,
  mutations,
  actions,
  getters
})

export default store;